package com.dao.impl;

import com.dao.BaseDao;
import com.dao.Supplier_infoDao;
import com.pojo.Supplier_info;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Supplier_infoDaoImpl implements Supplier_infoDao {

    private Connection connection = BaseDao.getConn();
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    /***
     * 获取每页供应商列表
     * @param pageIndex
     * @param pageSize
     * @param search
     * @return
     */
    @Override
    public List<Supplier_info> findPageSupplierList(int pageIndex, int pageSize, String search) {
        String sql = "select * from supplier_info where 1=1";
        if (!Objects.equals(search, "")) {
            sql += " and name like '%" + search + "%'";
        }
        sql += " and status = 0";
        sql += " limit " + pageSize + " offset " + pageSize * (pageIndex - 1);
        ArrayList<Supplier_info> supplierInfos = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Supplier_info supplierInfo = new Supplier_info();
                setSupplierAttr(supplierInfo, resultSet);
                supplierInfos.add(supplierInfo);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeAll(connection, preparedStatement, resultSet);
        }
        return supplierInfos;
    }

    /***
     * 获取供应商总数
     * @return
     */
    @Override
    public int findAllSupplierCount(String search) {
        String sql = "select count(1) from supplier_info where status = 0";
        if (!Objects.equals(search, "")) {
            sql += " and name like '%" + search + "%'";
        }
        return new Export_infoDaoImpl().queryCount(sql);
    }

    /***
     * 获取单个供应商信息、编辑页面
     * @param id
     * @return
     */
    @Override
    public Supplier_info findSupplierById(int id) {
        String sql = "select * from supplier_info where supplier_id = ?;";
        Supplier_info supplierInfo = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                supplierInfo = new Supplier_info();
                setSupplierAttr(supplierInfo, resultSet);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeAll(connection, preparedStatement, resultSet);
        }
        return supplierInfo;
    }

    /***
     * 编辑更新供应商
     * @param supplierInfo
     * @return
     */
    @Override
    public int updateSupplier(Supplier_info supplierInfo) {
        String sql = "update supplier_info set name = ?, contact_person = ?, contact_number = ?, email = ?, address = ?, status = ? where supplier_id = ?;";
        return BaseDao.ExcuteUpdate(sql, supplierInfo.getName(), supplierInfo.getContactPerson(), supplierInfo.getContactNumber(), supplierInfo.getEmail(),
                supplierInfo.getAddress(), supplierInfo.getStatus(), supplierInfo.getSupplierId());
    }

    /***
     * 添加供应商
     * @param supplierInfo
     * @return
     */
    @Override
    public int addSupplier(Supplier_info supplierInfo) {
        String sql = "insert into supplier_info values(null, ?, ?, ?, ?, ?, default);";
        return BaseDao.ExcuteUpdate(sql, supplierInfo.getName(), supplierInfo.getContactPerson(), supplierInfo.getContactNumber(),
                supplierInfo.getEmail(), supplierInfo.getAddress());
    }

    /***
     * 删除供应商 status 0是有效 1是无效
     * @param id
     * @return
     */
    @Override
    public int delSupplier(int id) {
        String sql = "update supplier_info set status = 1 where supplier_id = ?;";
        return BaseDao.ExcuteUpdate(sql, id);
    }

    @Override
    public int delSuppliers(String ids) {
        String sql = "update supplier_info set status = 1 where supplier_id in (";
        sql += ids;
        sql = sql.substring(0, sql.length() - 1);
        sql += ");";
        return BaseDao.ExcuteUpdate(sql);
    }

    /***
     * 公共方法设置供应商对象的属性
     * @param supplierInfo
     * @param resultSet
     */
    @Override
    public void setSupplierAttr(Supplier_info supplierInfo, ResultSet resultSet) throws SQLException {
        supplierInfo.setSupplierId(resultSet.getInt(1));
        supplierInfo.setName(resultSet.getString(2));
        supplierInfo.setContactPerson(resultSet.getString(3));
        supplierInfo.setContactNumber(resultSet.getString(4));
        supplierInfo.setEmail(resultSet.getString(5));
        supplierInfo.setAddress(resultSet.getString(6));
        supplierInfo.setStatus(resultSet.getInt(7));
    }

}
